package com.xiaoyg.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkDynamicProxy implements InvocationHandler {

    private Object targetObject;

    public JdkDynamicProxy(Object targetObject){
        super();
        this.targetObject = targetObject;
    }

    public Object newProxyInstance(){
        return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(),this.targetObject.getClass().getInterfaces(), this);
    }

    //代理类的方法调用都会被转发到该类的invoke()方法
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("JdkDynamicProxy, "+method.getName()+"方法，执行之前");
        return method.invoke(this.targetObject, args);
    }
}
